<template>
  <div>
    <div class="dispatcher-box">
      <div class="label">已设置调度员：</div>
      <div class="content" v-if="chooseList.length">
        <div class="item" v-for="(item, index) in chooseList" :key="index">{{ item.name }}</div>
      </div>
      <div class="content" v-else>暂无调度员</div>
      <div class="input-info">* 不勾选列表数据直接点确认，则为取消绑定车辆调度员</div>
    </div>
    <c-table
      ref="cTable"
      hasBorder
      noPage
      :size="size"
      :max-height="448"
      :loading="isLoading"
      :table-header="tableHeader"
      :table-list="tableList"
      :table-inner-btns="tableInnerBtns"
      @change-pagination="changePagination"
      @selection-handle="handleSelectionChange"
      @handle-selectall="handleSelectall"
    >
      <!-- <template v-slot:header>
        <c-search :form-model="searchObj" :form-items="searchItems" @submit-form="searchSubmit" @reset-form="searchReset"></c-search>
      </template> -->
    </c-table>
  </div>
</template>

<script>
import mixinTable from 'mixins/table'

export default {
  mixins: [mixinTable],
  props: {
    initData: {
      type: Object,
      default() {
        return []
      }
    }
  },
  data(vm) {
    return {
      dispatcherIds: [],
      curData: [],
      chooseList: [],
      tableList: [],
      tableInnerBtns: [],
      tableHeader: [
        {
          label: '调度员',
          prop: 'name',
          search: {
            type: 'input'
          }
        },
        {
          label: '手机号',
          prop: 'phone'
        },
        {
          label: '类型',
          prop: 'type',
          formatter: row => row && vm.setTableColumnLabel(row.type, 'dispatcherType')
        },
        {
          label: '状态',
          prop: 'status',
          setColor: row => (row.status === 0 ? 'red' : ''),
          formatter: row => row && vm.setTableColumnLabel(row.status, 'disabledStatus')
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      const { dateTime, ...other } = this.searchObj
      const searchDate = this.getSearchDate(dateTime)
      this.isLoading = true
      this.$api.vehicle
        .fetch('dispatcherList', {
          ...searchDate,
          ...other,
          pageNo: 1,
          pageSize: 100
        })
        .then(res => {
          this.isLoading = false
          if (res && res.totalCount) {
            const { data } = res
            this.tableList = data || []
          }
          this.queryChooseDispatcher()
        })
    },
    handleSelectall(item) {
      // this.chooseList = item
      this.dispatcherIds = item.map(res => res.id)
    },
    handleSelectionChange(item) {
      // this.chooseList = item
      this.dispatcherIds = item.map(res => res.id)
    },
    queryChooseDispatcher() {
      const { carNumber } = this.initData
      this.$api.vehicle.fetch('queryDispatcherByCarNumber', { carNumber }).then(res => {
        // this.$refs.cTable.toggleSelection(res)
        this.chooseList = res
      })
    }
  }
}
</script>

<style lang="less" scoped>
.dispatcher-box {
  display: flex;
  align-items: center;
  .label {
    width: 110px;
    color: @label-color;
  }
  .content {
    display: flex;
    flex-wrap: wrap;
    .item {
      padding: 5px 10px;
      margin: 5px 10px;
      border: 1px solid @border-default;
      border-radius: 4px;
    }
  }
}
</style>
